<?php

namespace common\entities\common;

use common\entities\ConfigEntity;
use Yii;

/**
 * 产品包和产品的关系
 * Class PackageToApplicationEntity
 * @package common\entities\common
 */
class MyscrmPackageToApplicationEntity extends ConfigEntity {

    public static function tableName() {
        return 'myscrm_packagetoapplication';
    }


    public function rules() {
        return [
            [['id', 'package_id', 'application_id'], 'required'],
            [['created_by', 'modified_by', 'id', 'package_id', 'application_id'], 'string', 'max' => 36],
        ];
    }

    public function attributeLabels() {
        return [
            'id' => 'ID',
            'package_id' => 'Package ID',
            'application_id' => 'Application ID',
            'created_by' => 'Created By',
            'modified_by' => 'Modified By',
            'created_on' => 'Created On',
            'modified_on' => 'Modified By',
        ];
    }

    /**
     * 获取包的授权
     * @param $organizationId
     * @param $contractId
     * @param $packageId
     * @return int
     */
    public static function getAuthorizes($organizationId, $contractId, $packageId){
        $pkgApps = MyscrmPackageToApplicationEntity::findAll([
            "package_id" => $packageId
        ]);
        if(!empty($pkgApps)){
            $auths = [];
            foreach($pkgApps as $pkgApp){
                $auth = MyscrmOrganizationToAuthorizeEntity::getAuthorize($organizationId, $contractId, $pkgApp['application_id']);
                if(!empty($auth)){
                    $auths []= $auth;
                }
            }
            return $auths;

        }
        return null;
    }

}
